package hu.arguscab.ai.game;

import hu.arguscab.ai.DecisionTree;
import hu.arguscab.ai.Model;
import hu.arguscab.ai.Node;
import hu.arguscab.ai.Path;
import hu.arguscab.game.CompoundAction;
import hu.arguscab.game.Ship;
import hu.arguscab.game.ShipState;
import hu.arguscab.game.WorldState;
import hu.arguscab.graphic.Drawable;
import hu.arguscab.graphic.Line;
import hu.arguscab.graphic.Triangle;
import hu.arguscab.physic.Vector;

/**
 *
 * @author sharp
 */
public class VisualDecisionTree implements Drawable{

	private DecisionTree tree;
	private ShipState ship;
	private Path<CompoundAction> path;

	public VisualDecisionTree() {

	}

	public void draw() {
		if ( tree.getRoot() == null )
			return;
		
		Triangle tr = new Triangle(10, 10);
		Line line = new Line();
		Node<Model> from = null;

		// vonalak
		line.setColor(0, 0, 1);
		for ( Node<Model> node : tree.breathSearchIterator() ) {
			WorldState wstate = (WorldState) node.getData().getState();
			ShipState shipState = wstate.getShipState();
			if ( (from=tree.getParent(node)) != null ) {
				Vector fromPos = ((WorldState)from.getData().getState()).getShipState().getPos();
				line.setPos(fromPos);
				line.setEndPoint(shipState.getPos());
				line.draw();
			}
		}
		// statek
		for ( Node<Model> node : tree.breathSearchIterator() ) {
			WorldState wstate = (WorldState) node.getData().getState();
			ShipState shipState = wstate.getShipState();
			float color = node.getData().getFitness()/1000;
			float red = 1 - color; // minél rosszabb, annál pirosabb
			float green = color; // minél jobb, annál zöldebb			tr.setColor(red, green, 0);
			tr.setColor(red, green, 0);
			tr.setPos( shipState.getPos() );
			tr.setAngle( (int)shipState.getDir().get() );
			tr.draw();
		}

		// Path
		line.setColor(1, 0, 1);
		tr.setColor(0, 1, 1);
		for (Node<Model> node : tree.bestSearchIterator()) {
			WorldState wstate = (WorldState) node.getData().getState();
			ShipState shipState = wstate.getShipState();
			tr.setPos( shipState.getPos() );
			tr.setAngle( (int)shipState.getDir().get() );
			tr.draw();
			// összekötni őket
			if ( (from=tree.getParent(node)) != null ) {
				Vector fromPos = ((WorldState)from.getData().getState()).getShipState().getPos();
				line.setPos(fromPos);
				line.setEndPoint(shipState.getPos());
				line.draw();
			}
		}

	}

	public void setTree(DecisionTree tree) {
		this.tree = tree;
	}

	public void setShip(Ship ship) {
		this.ship = ship.getState();
		this.tree = ((Bot)ship.getController()).getDecisionTree();
	}

	public void init() {
		/*this.startShip.setPos(new Vector(400, 300));
		WorldState now = new WorldState();
		now.setShipState( startShip );
		path = tree.makeDecision( new Model(now) );*/
	}
}
